package cn.iocoder.yudao.module.design.service.problemrecord;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.design.controller.admin.problemrecord.vo.*;
import cn.iocoder.yudao.module.design.convert.problemrecord.ProblemRecordConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.pic.PicDO;
import cn.iocoder.yudao.module.design.dal.dataobject.problemrecord.ProblemRecordDO;
import cn.iocoder.yudao.module.design.dal.mysql.problemrecord.ProblemRecordMapper;
import cn.iocoder.yudao.module.design.service.pic.PicService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.design.enums.ErrorCodeConstants.PROBLEM_RECORD_NOT_EXISTS;

/**
 * 问题记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ProblemRecordServiceImpl implements ProblemRecordService {
    @Resource
    private PicService picService;
    @Resource
    private ProblemRecordMapper problemRecordMapper;

    @Override
    public Long createProblemRecord(ProblemRecordCreateReqVO createReqVO) {
        // 插入
        ProblemRecordDO problemRecord = ProblemRecordConvert.INSTANCE.convert(createReqVO);
        problemRecord.setUserId(SecurityFrameworkUtils.getLoginUserId());
        problemRecord.setDeleted(false);
        problemRecordMapper.insert(problemRecord);

//        PicDO picDO = new PicDO();
//        picDO.setHouseId(problemRecord.getHouseId());
//        picDO.setFlowId(problemRecord.getFlowId());
//        picDO.setNodeId(problemRecord.getNodeId());
//        picDO.setItemId(problemRecord.getId());
//        picDO.setUploadType("0");
//        picDO.setSourceType("6");
//        // 保存图片文件
//        picService.savePicData(picDO, createReqVO.getFileList());
        // 返回
        return problemRecord.getId();
    }

    @Override
    public void updateProblemRecord(ProblemRecordUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateProblemRecordExists(updateReqVO.getId());
        // 更新
        ProblemRecordDO updateObj = ProblemRecordConvert.INSTANCE.convert(updateReqVO);
        PicDO picDO = new PicDO();
        picDO.setHouseId(updateReqVO.getHouseId());
        picDO.setFlowId(updateReqVO.getFlowId());
        picDO.setNodeId(updateReqVO.getNodeId());
        picDO.setItemId(updateReqVO.getId());
        picDO.setUploadType("0");
        picDO.setSourceType("6");
        // 保存图片文件
        picService.savePicData(picDO, updateReqVO.getFileList());
        problemRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteProblemRecord(Long id) {
        // 校验存在
        this.validateProblemRecordExists(id);
        // 删除
        problemRecordMapper.deleteById(id);
    }

    private void validateProblemRecordExists(Long id) {
        if (problemRecordMapper.selectById(id) == null) {
            throw exception(PROBLEM_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ProblemRecordDO getProblemRecord(Long id) {
        return problemRecordMapper.selectById(id);
    }

    @Override
    public ProblemRecordRespVO getProblemRecordData(Long id) {
        ProblemRecordDO problemRecordDO = problemRecordMapper.selectById(id);
        ProblemRecordRespVO problemRecordRespVO = ProblemRecordConvert.INSTANCE.convert(problemRecordDO);
        // 查询 图片数据
        // 查询上传附件,图片1
        PicDO picDO = new PicDO();
        picDO.setHouseId(problemRecordRespVO.getHouseId());
        picDO.setFlowId(problemRecordRespVO.getFlowId());
        picDO.setNodeId(problemRecordRespVO.getNodeId());
        picDO.setItemId(problemRecordRespVO.getId());
        picDO.setUploadType("0");
        picDO.setSourceType("6");
        List<PicDO> picData = picService.getPicData(picDO);
        return problemRecordRespVO;
    }

    @Override
    public List<ProblemRecordDO> getProblemRecordList(Collection<Long> ids) {
        return problemRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ProblemRecordDO> getProblemRecordPage(ProblemRecordPageReqVO pageReqVO) {
        return problemRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProblemRecordDO> getProblemRecordList(ProblemRecordExportReqVO exportReqVO) {
        return problemRecordMapper.selectList(exportReqVO);
    }

}
